package com.hlkj.baojin.service.impl;

import com.google.common.collect.Maps;
import com.hlkj.baojin.constant.BaojinConstant;
import com.hlkj.baojin.dao.ICompanyAreaDao;
import com.hlkj.baojin.dao.repository.CompanyAreaRepository;
import com.hlkj.baojin.entity.CompanyArea;
import com.hlkj.baojin.entity.Device;
import com.hlkj.baojin.entity.dto.CompanyAreaDto;
import com.hlkj.baojin.entity.dto.CompanyAreaSearchDto;
import com.hlkj.baojin.entity.dto.ModelList;
import com.hlkj.baojin.entity.dto.MonitorDto;
import com.hlkj.baojin.jwt.JwtUser;
import com.hlkj.baojin.service.CompanyService;
import com.hlkj.baojin.service.DeviceService;
import com.hlkj.baojin.service.ICompanyAreaService;
import com.hlkj.baojin.service.IUserService;
import com.hlkj.baojin.utils.ErrororUtil;
import com.hlkj.baojin.utils.MyLists;
import com.hlkj.baojin.utils.bean.CopyUtil;
import com.hlkj.baojin.utils.bean.JsonResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author HULIN
 * @created with: baojin
 * @package com.hlkj.baojin.service.impl
 * @description:
 * @date 2019/1/9 10:48
 * @modified By:
 * @Copyright © 2018 HAILIANG Info. Tech Ltd. All rights reserved.
 */
@Service
public class CompanyAreaServiceImpl implements ICompanyAreaService {

    @Autowired
    private IUserService userService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private ICompanyAreaDao companyAreaDao;
    @Autowired
    private CompanyAreaRepository companyAreaRepository;
    @Autowired
    private DeviceService deviceService;

    @Override
    public ICompanyAreaDao getCompanyAreaDao() {
        return this.companyAreaDao;
    }

    @Override
    @Transactional
    public void saveCompanyArea(CompanyAreaDto companyAreaDto) {
        JwtUser currentUser = userService.getCurrentUser();

        CompanyArea companyAreaDB = companyAreaDao.findByBelongCompanyAndNameAndDelete(companyAreaDto.getBelongCompany(),
                companyAreaDto.getName(), BaojinConstant.IS_NO_DELETE);
        ErrororUtil.isFalseNull(companyAreaDB, "保存厂区", "厂区名称已存在");

        CompanyArea companyArea = new CompanyArea(currentUser.getId(), System.currentTimeMillis(), 0, BaojinConstant.IS_NO_DELETE);
        BeanUtils.copyProperties(companyAreaDto, companyArea, CopyUtil.getNullPropertyNames(companyAreaDto));
        companyAreaDao.save(companyArea);
    }

    @Override
    @Transactional
    public void updateCompanyArea(CompanyAreaDto companyAreaDto) {

        companyAreaDto.setBelongCompany(null);
        if(companyAreaDto.getName()!= null && StringUtils.isBlank(companyAreaDto.getName())) {
            companyAreaDto.setName(null);
        }
        JwtUser currentUser = userService.getCurrentUser();

        CompanyArea companyArea = companyAreaDao.findByIdAndDelete(companyAreaDto.getId(), BaojinConstant.IS_NO_DELETE);
        ErrororUtil.isTrueNull(companyArea, "更新厂区", "不存在所更新厂区");

        CompanyArea companyAreaDB = companyAreaDao.findByBelongCompanyAndNameAndDelete(companyAreaDto.getBelongCompany(),
                companyAreaDto.getName(), BaojinConstant.IS_NO_DELETE);
        ErrororUtil.isTrue(companyAreaDB != null &&
                !companyAreaDB.getId().equals(companyArea.getId()) &&
                companyAreaDB.getName().equals(companyArea.getName()), "保存厂区", "厂区名称已存在");


        BeanUtils.copyProperties(companyAreaDto, companyArea, CopyUtil.getNullPropertyNames(companyAreaDto));
        companyArea.setUpdateMan(currentUser.getId());
        companyArea.setUpdateTime(System.currentTimeMillis());

        companyAreaDao.save(companyArea);
    }

    @Override
    @Transactional
    public void deleteCompanyArea(String companyAreaId) {
        CompanyArea companyArea = companyAreaDao.findByIdAndDelete(companyAreaId, BaojinConstant.IS_NO_DELETE);
        ErrororUtil.isTrueNull(companyArea, "删除厂区", "不存在所删除厂区");

        List<Device> deviceErrorList = deviceService.getDeviceDao().findByCompanyAreaAndStatusAndDelete(companyAreaId, 1, BaojinConstant.IS_NO_DELETE);
        ErrororUtil.isTrue(MyLists.notNullAndEmpty(deviceErrorList), "删除厂区", "存在关联的设备处于故障状态, 无法删除");

        JwtUser currentUser = userService.getCurrentUser();
        CompanyArea companyAreaDelete = new CompanyArea(companyArea.getId(), currentUser.getId(), System.currentTimeMillis(), BaojinConstant.IS_DELETE);
        companyAreaRepository.updateDeviceNotNull(companyAreaDelete);
        List<Device> deviceList = deviceService.getDeviceDao().findByCompanyAreaAndDelete(companyAreaId, BaojinConstant.IS_NO_DELETE);
        deviceList.stream().forEach(device -> deviceService.deleteDevice(device.getId()));
    }

    @Override
    public List<CompanyAreaDto> findCompanyAreaList(String companyId) {

        List<CompanyArea> companyAreaList = companyAreaDao.findByBelongCompanyAndDelete(companyId, BaojinConstant.IS_NO_DELETE);
        List<CompanyAreaDto> companyAreaDtoList = new ArrayList<>();

        for (CompanyArea companyArea : companyAreaList) {
            CompanyAreaDto companyAreaDto = new CompanyAreaDto();
            companyAreaDto.setId(companyArea.getId());
            companyAreaDto.setName(companyArea.getName());
            companyAreaDtoList.add(companyAreaDto);
        }
        return companyAreaDtoList;
    }

    @Override
    public List<MonitorDto> findMonitorByCompanyAreaId(String companyAreaId) {

        CompanyArea companyArea = companyAreaDao.findByIdAndDelete(companyAreaId, BaojinConstant.IS_NO_DELETE);
        ErrororUtil.isTrueNull(companyArea, "查找厂区监控", "厂区不存在");

        return companyArea.getMonitorDtoList();
    }

    @Override
    public JsonResult findCompanyArea(CompanyAreaSearchDto companyAreaSearchDto) {
        ModelList<CompanyArea> companyAreaModelList = companyAreaRepository.getCompanyArea(companyAreaSearchDto);

        List<CompanyArea> companyAreaList = companyAreaModelList.getList();

        List<CompanyAreaDto> companyAreaDtoList = CopyUtil.copy(companyAreaList, CompanyAreaDto.class);
        companyAreaDtoList.forEach(companyAreaDto ->
                companyAreaDto.setBelongCompanyName(companyService.getCompanyNameById(companyAreaDto.getBelongCompany())));

        Map maps = Maps.newHashMap();
        maps.put("list", companyAreaDtoList);
        maps.put("count", companyAreaModelList.getCount());

        return JsonResult.success(maps);
    }

    @Override
    public String getCompanyAreaNameByCompanyAreaId(String companyAreaId) {

        CompanyArea companyArea = companyAreaDao.findByIdAndDelete(companyAreaId, BaojinConstant.IS_NO_DELETE);
        ErrororUtil.isTrueNull(companyArea, "厂区查找", "数据异常");

        return companyArea.getName();
    }
}
